﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FanPages.Server.Services.Contracts;
using FanPages.Server.Services;
using FanPages.Model;
using FanPages.Client.Core.Transformers;
using FanPagesWeb.Areas.Phanatio.Models;

namespace FanPagesWeb.Areas.Phanatio.Controllers
{
    public class HomeController : Controller
    {
        IFanItemService fanItemService = new FanItemService();
        IFixedLinkService linkService = new FixedLinkService();
        INewsService newsService = new NewsService();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ViewFan(string id)
        {
            var dto = fanItemService.GetByCode(id);
            var linksDto = linkService.GetByFanItem(dto);
            var newsDto = newsService.GetByFanItem(dto);
            FanItem fanItem = null;
            List<FixedLink> links = null;
            List<SearchResult> searchs = null;

            if (dto != null)
            {
                fanItem = new FanItemTransformer().ToClient(dto);
                links = new FixedLinkTransformer().ToListEntity(linksDto);
                searchs = new SearchResultTransformer().ToListEntity(newsDto);
            }
            FanItemView fanItemView = new FanItemView()
            {
                FanItem = fanItem,
                Links = links,
                News = searchs
            };
            return View(fanItemView);
        }

        public ActionResult Fan(string id)
        {
            return RedirectToAction("ViewFan", new { id = id });
        }

        public ActionResult GetImage(int id)
        {
            var item = fanItemService.GetById(id);
            if (item.Image != null)
                return File(item.Image, "image/jpg");
            return null;
        }
    }   
}
